package offer;

public class offer_0025 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null&&l2==null) return null;
        ListNode head=new ListNode(0);
        ListNode p=head;
        while(l1!=null||l2!=null){
            if(l1!=null&&l2!=null){
                if(l1.val<=l2.val){
                    p.next=new ListNode(l1.val);
                    l1=l1.next;
                }
                else {p.next=new ListNode(l2.val);
                        l2=l2.next;
                }
            }
            else if(l1==null){
                p.next=new ListNode(l2.val);
                l2=l2.next;
            }
            else {
                p.next=new ListNode(l1.val);
                l1=l1.next;
            }
            p=p.next;
        }
        return head.next;
    }
}
